package LuceneIndexCreate;

import com.alibaba.fastjson.JSON;
import LuceneIndexCreate.model.CatData;
import LuceneIndexCreate.model.CatIdsFrom1688;
import LuceneIndexCreate.model.CatIndexData;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by User on 2017/12/6.
 */
public class App {

    static Boolean isStop = false;

    public static void main(String[] args) throws Exception {

        getData5();

    }

    private static void getData() {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(32);
        for (int page = 0; page < 110; page++) {
            final int fpage = page;
            fixedThreadPool.execute(() -> {
                if (isStop) return;
                try {
                    startJob(fpage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

        new Thread(()->{
            for(;;) {
                Scanner scanner = new Scanner(System.in);
                String str = scanner.next();
                if ("stop".equals(str)) {
                    System.out.println("正在执行的线程执行结束后停止");
                    isStop = true;
                    break;
                }
            }
        }).start();
    }

    public static void startJob(int page) throws Exception {
        FileOutputStream outWriter1 = createOutWriter("编号数据1\\测试" + page + ".txt");
        FileOutputStream outWriter2 = createOutWriter("索引数据1\\测试" + page + ".txt");

        // 从数据库中获取数据 100个
        List<CatData> cidAndNameFromDB = DB.getCidAndNameFromDB(page);
        int count = cidAndNameFromDB.size();
        if (count <= 0) return;;
        int doneCount = 0;
        for (CatData catData : cidAndNameFromDB) {
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            String jsonString = JSON.toJSONString(catIdsFrom1688);
            appendToFile(outWriter1, jsonString);
            CatIndexData indexData = GetDesc.getDate(catIdsFrom1688);
            String indexDataJson = JSON.toJSONString(indexData);
            appendToFile(outWriter2, indexDataJson);
            doneCount++;
            System.out.println("第" + page + "页:" + doneCount + "/" + count);
        }

        outWriter1.close();
        outWriter2.close();
    }

    public static FileOutputStream createOutWriter(String fileName) throws FileNotFoundException {
        String path = "E:\\lucene";
        String filePath = path + "\\" + fileName;
        File file = new File(filePath);
        if (file.exists()) file.delete();
        if(!file.getParentFile().exists()) {
            if(!file.getParentFile().mkdirs()) {
                System.out.println("创建目标文件所在目录失败！");
                return createOutWriter(fileName);
            }
        }

        FileOutputStream out = new FileOutputStream(filePath, true);
        return out;
    }

    public static void appendToFile(FileOutputStream out, String string) throws IOException {
        string += "\r\n";
        out.write(string.getBytes());
        out.flush();
    }

    public static void getData1() throws IOException, InterruptedException {
        FileOutputStream outWriter1 = createOutWriter("编号数据2\\女装_女士精品.txt");
        FileOutputStream outWriter2 = createOutWriter("索引数据2\\女装_女士精品.txt");

        List<Map> exec = DB.exec("select * from item_cat_lucene2 where nnn like '%女装/女士精品%'");
        for (Map map : exec) {
            String cid = String.valueOf(map.get("cid"));
            String nnn = String.valueOf(map.get("nnn"));
            int i = nnn.lastIndexOf("-->");
            nnn = i > 0 ? nnn.substring(i+3) : nnn;
            String keyword = "女装 " + nnn;
            System.out.println(cid + " " + keyword);
            CatData catData = new CatData(cid, keyword);
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            String jsonString = JSON.toJSONString(catIdsFrom1688);
            appendToFile(outWriter1, jsonString);
            CatIndexData indexData = GetDesc.getDate(catIdsFrom1688);
            String indexDataJson = JSON.toJSONString(indexData);
            appendToFile(outWriter2, indexDataJson);
        }

        outWriter1.close();
        outWriter2.close();
    }

    public static void getData2() throws IOException, InterruptedException {
        FileOutputStream outWriter1 = createOutWriter("编号数据2\\男装.txt");
        FileOutputStream outWriter2 = createOutWriter("索引数据2\\男装.txt");

        List<Map> exec = DB.exec("select * from item_cat_lucene2 where nnn like '%男装-->%'");
        for (Map map : exec) {
            String cid = String.valueOf(map.get("cid"));
            String nnn = String.valueOf(map.get("nnn"));
            if (!nnn.startsWith("男装")) continue;
            int i = nnn.lastIndexOf("-->");
            nnn = i > 0 ? nnn.substring(i+3) : nnn;
            String keyword = "男装 " + nnn;
            System.out.println(cid + " " + keyword);
            CatData catData = new CatData(cid, keyword);
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            String jsonString = JSON.toJSONString(catIdsFrom1688);
            appendToFile(outWriter1, jsonString);
            CatIndexData indexData = GetDesc.getDate(catIdsFrom1688);
            String indexDataJson = JSON.toJSONString(indexData);
            appendToFile(outWriter2, indexDataJson);
        }

        outWriter1.close();
        outWriter2.close();
    }

    public static void getData3() throws IOException, InterruptedException {
        FileOutputStream outWriter1 = createOutWriter("编号数据2\\女鞋.txt");
        FileOutputStream outWriter2 = createOutWriter("索引数据2\\女鞋.txt");

        List<Map> exec = DB.exec("select * from item_cat_lucene2 where nnn like '%女鞋-->%'");
        for (Map map : exec) {
            String cid = String.valueOf(map.get("cid"));
            String nnn = String.valueOf(map.get("nnn"));
            if (!nnn.startsWith("女鞋")) continue;
            int i = nnn.lastIndexOf("-->");
            nnn = i > 0 ? nnn.substring(i+3) : nnn;
            String keyword = "女鞋 " + nnn;
            System.out.println(cid + " " + keyword);
            CatData catData = new CatData(cid, keyword);
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            String jsonString = JSON.toJSONString(catIdsFrom1688);
            appendToFile(outWriter1, jsonString);
            CatIndexData indexData = GetDesc.getDate(catIdsFrom1688);
            String indexDataJson = JSON.toJSONString(indexData);
            appendToFile(outWriter2, indexDataJson);
        }

        outWriter1.close();
        outWriter2.close();
    }

    public static void getData4() throws IOException, InterruptedException {
        FileOutputStream outWriter1 = createOutWriter("编号数据2\\流行男鞋.txt");
        FileOutputStream outWriter2 = createOutWriter("索引数据2\\流行男鞋.txt");

        List<Map> exec = DB.exec("select * from item_cat_lucene2 where nnn like '%流行男鞋-->%'");
        for (Map map : exec) {
            String cid = String.valueOf(map.get("cid"));
            String nnn = String.valueOf(map.get("nnn"));
            if (!nnn.startsWith("流行男鞋")) continue;
            int i = nnn.lastIndexOf("-->");
            nnn = i > 0 ? nnn.substring(i+3) : nnn;
            String keyword = "男鞋 " + nnn;
            System.out.println(cid + " " + keyword);
            CatData catData = new CatData(cid, keyword);
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            String jsonString = JSON.toJSONString(catIdsFrom1688);
            appendToFile(outWriter1, jsonString);
            CatIndexData indexData = GetDesc.getDate(catIdsFrom1688);
            String indexDataJson = JSON.toJSONString(indexData);
            appendToFile(outWriter2, indexDataJson);
        }

        outWriter1.close();
        outWriter2.close();
    }


    public static void getData5() throws IOException, InterruptedException {
        FileOutputStream outWriter1 = createOutWriter("编号数据2\\童装童鞋亲子装.txt");
        FileOutputStream outWriter2 = createOutWriter("索引数据2\\童装童鞋亲子装.txt");

        List<Map> exec = DB.exec("select * from item_cat_lucene2 where nnn like '%童装/童鞋/亲子装-->%'");
        for (Map map : exec) {
            String cid = String.valueOf(map.get("cid"));
            String nnn = String.valueOf(map.get("nnn"));
            if (!nnn.startsWith("童装/童鞋/亲子装")) continue;
            int i = nnn.lastIndexOf("-->");
            nnn = i > 0 ? nnn.substring(i+3) : nnn;
            String keyword = "童装 " + nnn;
            System.out.println(cid + " " + keyword);
            CatData catData = new CatData(cid, keyword);
            CatIdsFrom1688 catIdsFrom1688 = NumiidGet.requestMoblieCatIdsFrom1688ByCatData(catData);
            String jsonString = JSON.toJSONString(catIdsFrom1688);
            appendToFile(outWriter1, jsonString);
            CatIndexData indexData = GetDesc.getDate(catIdsFrom1688);
            String indexDataJson = JSON.toJSONString(indexData);
            appendToFile(outWriter2, indexDataJson);
        }

        outWriter1.close();
        outWriter2.close();
    }



}
